# Copyright 2022 CloudWeGo Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

all: thrift.peg.go AST.go ../plugin/protocol.go

thrift.peg.go: thrift.peg
	which peg >/dev/null || go install github.com/pointlander/peg@latest
	peg ./thrift.peg
	echo '// Code generated by peg. DO NOT EDIT.' > tmp && cat thrift.peg.go >> tmp && mv tmp thrift.peg.go

AST.go: AST.thrift
	thriftgo --gen go:use_type_alias=false,template=slim,gen_type_meta \
		AST.thrift && mv gen-go/parser/AST.go AST.go && rm -r gen-go
	#sed -i '/func (p \*\(Type\|ConstValue\)) String() string/,+6d' AST.go # GNU sed
	sed -i'.backup' -E '/func \(p \*(Type|ConstValue)\) String\(\) string/{N;N;N;N;N;N;d;}' AST.go && rm AST.go.backup

../plugin/protocol.go: ../plugin/protocol.thrift
	thriftgo --gen go:package_prefix=github.com/cloudwego/thriftgo/,template=slim,gen_type_meta \
		../plugin/protocol.thrift && mv gen-go/plugin/protocol.go ../plugin/ && rm -r gen-go
